//
//  MyNetworking.m
//  装13小视频
//
//  Created by Bensn on 2017/3/11.
//  Copyright © 2017年 change360. All rights reserved.
//

#import "MyNetworking.h"
#import "NSString+Appending.h"

#define XSPURL @"http://192.168.1.188/XSP/Api/"

@implementation MyNetworking

+ (instancetype)sharedManager{
    
    static MyNetworking *manager = nil;
    static dispatch_once_t pred;
    dispatch_once(&pred, ^{
        
        manager = [[self alloc] initWithBaseURL:[NSURL URLWithString:XSPURL]];
        
    });
    
    return manager;
    
}
- (instancetype)initWithBaseURL:(NSURL *)url
{
    self = [super initWithBaseURL:url];
    if (self) {
        // 请求超时设定
        self.requestSerializer.timeoutInterval = 5;
        self.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
        [self.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [self.requestSerializer setValue:url.absoluteString forHTTPHeaderField:@"Referer"];
        
        self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/plain", @"text/javascript", @"text/json", @"text/html", nil];
        
        self.securityPolicy.allowInvalidCertificates = YES;
    }
    return self;
}
- (void)requestWithMethod:(HTTPMethod)method
                 WithPath:(NSString *)path
           WithDictParams:(NSDictionary*)params
         WithSuccessBlock:(requestSuccessBlock)success
          WithFailurBlock:(requestFailureBlock)failure
{
    switch (method) {
        case GET:{
            [self GET:path parameters:params progress:nil success:^(NSURLSessionTask *task, NSDictionary * responseObject) {
                success(responseObject);
            } failure:^(NSURLSessionTask *operation, NSError *error) {
                
                failure(error);
            }];
            break;
        }
        case POST:{
            [self POST:path parameters:params progress:nil success:^(NSURLSessionTask *task, NSDictionary * responseObject) {
                success(responseObject);
            } failure:^(NSURLSessionTask *operation, NSError *error) {
                
                failure(error);
            }];
            
            break;
        }
        default:
            break;
    }
}

/**
 登录

 @param userName 用户名
 @param password 密码
 @param success 成功回调
 @param failure 失败回调
 */
- (void) logInWithUserName:(NSString *)userName password:(NSString *)password WithSuccessBlock:(requestSuccessBlock)success WithFailurBlock:(requestFailureBlock)failure{
    //[XSPURL appendingWith:@"User/login"]
    [self requestWithMethod:POST WithPath:@"User/login" WithDictParams:@{@"name":userName, @"pwd":password} WithSuccessBlock:success WithFailurBlock:failure];
}

/**
 注册
 
 @param userName 用户名
 @param password 密码
 @param success 成功回调
 @param failure 失败回调
 */
- (void) registerWithUserName:(NSString *)userName password:(NSString *)password WithSuccessBlock:(requestSuccessBlock)success WithFailurBlock:(requestFailureBlock)failure{
    
    [self requestWithMethod:POST WithPath:@"User/reg" WithDictParams:@{@"phone":userName, @"pwd":password} WithSuccessBlock:success WithFailurBlock:failure];
}

/**
 忘记密码
 
 @param userName 用户名
 @param password 密码
 @param success 成功回调
 @param failure 失败回调
 */
- (void) forgetPasswordWithUserName:(NSString *)userName password:(NSString *)password WithSuccessBlock:(requestSuccessBlock)success WithFailurBlock:(requestFailureBlock)failure{
    
    [self requestWithMethod:POST WithPath:@"user/changePwd" WithDictParams:@{@"name":userName, @"pwd":password} WithSuccessBlock:success WithFailurBlock:failure];
}

/**
 获取热门城市分类

 @param success 成功回调
 @param failure 失败回调
 */
- (void) obtainCityClassWithSuccessBlock:(requestSuccessBlock)success WithFailurBlock:(requestFailureBlock)failure{
    
    [self requestWithMethod:GET WithPath:@"Video/getaddressId" WithDictParams:nil WithSuccessBlock:success WithFailurBlock:failure];
}


/**
 获取推荐视频列表

 @param page 分页
 @param success 成功回调
 @param failure 失败回调
 */
- (void) getrecommendvideoPage:(NSInteger)page WithSuccessBlock:(requestSuccessBlock)success WithFailurBlock:(requestFailureBlock)failure{
    
    [self requestWithMethod:POST WithPath:@"Video/getrecommendvideo" WithDictParams:[self patient_idDict:@{@"page":@(page)}] WithSuccessBlock:success WithFailurBlock:failure];
    
}

/**
 查询个人资料
 @param success 成功回调
 @param failure 失败回调
 */
-(void)getPersonInfo:(NSInteger)userID token:(NSString *)token logID:(NSInteger)logID WithSuccessBlock:(requestSuccessBlock)success WithFailurBlock:(requestFailureBlock)failure{
    [self requestWithMethod:POST WithPath:@"User/findUser" WithDictParams:@{@"user_id":@(userID), @"id":@(logID),@"token":token} WithSuccessBlock:success WithFailurBlock:failure];
}
/**
 上传个人资料
 @param true_name真实姓名
 @param nick 昵称
 @param age  年龄
 @param birthday 出生年月
 @param gender 性别
 @param success 成功回调
 @param failure 失败回调
 */
-(void)upPersonBaseInfo:(NSInteger)userID token:(NSString *)token true_name:(NSString *)true_name nick:(NSString *)nick age:(NSInteger)age birthday:(NSString *)birthday gender:(NSInteger)gender WithSuccessBlock:(requestSuccessBlock)success WithFailurBlock:(requestFailureBlock)failure{

    [self requestWithMethod:POST WithPath:@"User/editUserInfo" WithDictParams:@{
                                                                                @"id":@(userID),
                                                                                @"token":token,
                                                                                @"true_name":true_name,
                                                                                @"nick":nick,
                                                                                @"age":@(age),
                                                                                @"birthday":birthday
                                                                                 } WithSuccessBlock:success WithFailurBlock:failure];

}



//参数是user_id、token
- (NSDictionary *)patient_idDict:(NSDictionary *)infoDic{
    
    NSMutableDictionary *mutableDic = [NSMutableDictionary dictionaryWithDictionary:@{@"user_id":[self getUserId],@"token":[self getToken]}];
    
    if (infoDic != nil) {
        
        [mutableDic addEntriesFromDictionary:infoDic];
    }
    
    return mutableDic;
}
//参数是id、token
- (NSDictionary *)idDict:(NSDictionary *)infoDic{
    
    
    NSMutableDictionary *mutableDic = [NSMutableDictionary dictionaryWithDictionary:@{@"id":[self getUserId],@"token":[self getToken]}];
    
    if (infoDic != nil) {
        
        [mutableDic addEntriesFromDictionary:infoDic];
    }
    
    return mutableDic;
}
//本地ID
- (NSString *)getUserId{
    
    return [self getUseDefaults:@"id"];
}
//本地token
- (NSString *)getToken{
    return [self getUseDefaults:@"token"];
}


- (NSString *)getUseDefaults:(NSString *)key{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *value = [userDefaults objectForKey:key];
    if (value == nil) {
        value = @"";
    }
    return value;
}
@end
